package com.jied.duodian.web;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.jied.duodian.entity.DdOrder;
import com.jied.duodian.entity.Dutity.Echarts;
import com.jied.duodian.service.*;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/11/7
 * WWW https://github.com/j6l
 */
@RestController
@RequestMapping("/adminorder")
@Slf4j
public class AdminOrderController {

    @Autowired
    DdOrderService ddOrderService;
    @Autowired
    DdShoppingService ddShoppingService;
    @Autowired
    SuId suId;
    @Autowired
    DdAddressService ddAddressService;
    @Autowired
    DdProductService ddProductService;
    @Autowired
    DdUserService ddUserService;


    /**
     * 获取所有订单
     *
     * @return
     */
    @GetMapping("/getlist")
    public CommonResult getlist() {
        List<DdOrder> orderBy = ddOrderService.getOrder();
        return CommonResult.success( orderBy );
    }

    /**
     * 获取当前用户订单
     *
     * @param req
     * @return
     */
    @GetMapping("/getBynamelist")
    public CommonResult getBynamelist(HttpServletRequest req) {
        List<DdOrder> orderBy = ddOrderService.getallbyusername( suId.getuserName( req ) );
        return CommonResult.success( orderBy );
    }

    @GetMapping("/delorder/{id}")
    public CommonResult delorder(@PathVariable("id") Integer id) {
        Integer orderBy = ddOrderService.delorderbyid( id );
        return CommonResult.success( orderBy );
    }

    /**
     * 更新发货地址
     *
     * @param ddOrder
     * @return
     */
    @PostMapping("/updataorderfa")
    public CommonResult updataorder(@RequestBody DdOrder ddOrder) {
        DdOrder order = new DdOrder();
        order.setId( ddOrder.getId() );
        order.setFaAddress( ddOrder.getFaAddress() );
        order.setDeliverySn( ddOrder.getDeliverySn() );
        Integer integer = ddOrderService.updataOrderByid( order );
        return CommonResult.success( integer );
    }

    /**
     * @return
     */
    @PostMapping("/getxtsj")
    public CommonResult getxtsj() {
        HashMap<String, Object> map = new HashMap<>();
        Long integerdsh = ddProductService.getProductCuntDsh();
        Long integerdsj = ddProductService.getProductCuntDsj();
        Long orderzt = ddOrderService.getOrderzt();
        map.put( "productdsh", integerdsh );
        map.put( "productdsj", integerdsj );
        map.put( "orderzt", orderzt );
        Long ordernopay = ddOrderService.getOrdernopay();
        Long ordernofa = ddOrderService.getOrdernofa();
        map.put( "ordernopay", ordernopay );
        map.put( "ordernofa", ordernofa );
        Long userall = ddUserService.getuserall();
        Long userzt = ddUserService.getuserzt();
        Long usernojihuo = ddUserService.getusernojihuo();
        map.put( "userall", userall );
        map.put( "userzt", userzt );
        map.put( "usernojihuo", usernojihuo );
        log.info( "输出系统数据{}", map );
        return CommonResult.success( map );
    }


    /**
     * 监控数据的本月订单详情
     *
     * @return
     */
    @GetMapping("/getbenyue")
    public CommonResult getbenyue() {
        ArrayList<Echarts> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        //本月第一天
        LocalDate firstday = LocalDate.of( localDate.getYear(), localDate.getMonthValue(), 1 );
        //本月最后一天
        LocalDate lastDay = localDate.with( TemporalAdjusters.lastDayOfMonth() );

        for (long i = 0; i < lastDay.getDayOfMonth(); i++) {
            long orderBy = ddOrderService.getmonth( firstday );
            Echarts echarts = new Echarts();
            echarts.setTian( firstday.toString() );
            echarts.setSum( Math.toIntExact( orderBy ) );
            firstday = firstday.plusDays( 1l );
            list.add( echarts );
        }
        log.info( "请求了{}天的数据,数据为{}", lastDay.getDayOfMonth(), list );
        return CommonResult.success( list );
    }
}
